# Maintainer: Daurnimator <daurnimator@archlinux.org>

pkgname=('grafana-agent' 'grafana-agentctl' 'grafana-agent-crow')
pkgver=0.33.0
pkgrel=1
pkgdesc='Grafana Agent is a telemetry collector for sending metrics, logs, and trace data to the opinionated Grafana observability stack'
url='https://github.com/grafana/agent'
arch=('loong64' 'x86_64')
license=('Apache')
depends=('glibc')
makedepends=('go' 'systemd')
options=('!lto')
source=($pkgname-$pkgver.tar.gz::https://github.com/grafana/agent/archive/v$pkgver.tar.gz
        grafana-agent.sysusers grafana-agent.service grafana-agent.tmpfiles)
sha512sums=('85a7467f27df08c439ed4685d0ff36ac2095a0845795b4f8b693817e080d07956959896d6507469103982ed9fef5b54acb0f650a409802d6e977ec15e8ad49ca'
            '1e58f6273562fd6ddeae41bb6d223230ed301199af6bd2f85fa3d2c1e6352952c600cd4488ad769069519c42b6863be84aaa2c93c88f696e5a0bedbb93758d0e'
            '3f8debbc4732009f54c063d70c2dce4846b81b829f495d25a431fef857077c83949d777f763de8c106fb8d1cf0bd7e874680db8387b4811704b1a303932fa090'
            '3a4c4896b2454272b5a2d53ba5aa7009fa5b42a573fef521afb5d9712e53119539c4195d2b149d04d086c5fa197ccf7dc9bbdc407efb55514e3c92b994121fa8')

prepare() {
  cd "agent-$pkgver"

  export GOPATH="${srcdir}"
  go mod download
}

build() {
  cd "agent-$pkgver"

  export CGO_LDFLAGS="${LDFLAGS}"
  export CGO_CFLAGS="${CFLAGS}"
  export CGO_CPPFLAGS="${CPPFLAGS}"
  export CGO_CXXFLAGS="${CXXFLAGS}"
  export GOPATH="${srcdir}"
  export GOPROXY=off
  for path in cmd/grafana-agent cmd/grafana-agentctl tools/crow; do
    go build \
      -trimpath \
      -buildmode=pie \
      -mod=readonly \
      -modcacherw \
      -tags promtail_journal_enabled \
      -ldflags " \
         -X github.com/grafana/agent/pkg/build.Version=$pkgver
         -X github.com/grafana/agent/pkg/build.BuildDate=$(date -u +'%Y-%m-%dT%H:%M:%SZ' --date=@${SOURCE_DATE_EPOCH})
         -compressdwarf=false
         -linkmode external
         -extldflags \"${LDFLAGS}\"" \
      "./$path"
  done
}

check() {
  cd "agent-$pkgver"

  ./grafana-agent -version
  ./grafana-agentctl --version

  go test -v ./pkg/{agentctl,config,crow,logs,metrics,server,traces}/
}

package_grafana-agent() {
  backup=('etc/grafana-agent.yaml')

  cd "agent-$pkgver"

  install -Dm755 -t "$pkgdir/usr/bin" grafana-agent
  install -Dm644 packaging/grafana-agent/grafana-agent.yaml "$pkgdir/etc/grafana-agent.yaml"

  install -Dm644 "$srcdir/grafana-agent.sysusers" "$pkgdir/usr/lib/sysusers.d/grafana-agent.conf"
  install -Dm644 "$srcdir/grafana-agent.tmpfiles" "$pkgdir/usr/lib/tmpfiles.d/grafana-agent.conf"
  install -Dm644 "$srcdir/grafana-agent.service" "$pkgdir/usr/lib/systemd/system/grafana-agent.service"
}

package_grafana-agentctl() {
  pkgdesc="provides utilities for interacting with Grafana Agent"

  cd "agent-$pkgver"

  install -Dm755 -t "$pkgdir"/usr/bin grafana-agentctl
}

package_grafana-agent-crow() {
  pkgdesc="A correctness checker tool that validates that scraped metrics are delivered to a remote_write endpoint."

  cd "agent-$pkgver"

  install -Dm755 crow "$pkgdir"/usr/bin/grafana-agent-crow
}
